package com.lunatic.rocketmq.demo;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;
import java.util.Random;

public class SqlFilterProducer {
    public static void main(String[] args) throws Exception {
        // 1. 初始化生产者
        DefaultMQProducer producer = new DefaultMQProducer(
                "producer_group1");
        producer.setNamesrvAddr("192.168.101.106:9876");
        producer.start();

        String[] s = new String[] {"TagF","TagA","TagB", "TagC"};
        // 2. 发送消息（使用 Java 17 特性：var 关键字）
        for (int i = 0; i < 100; i++) {
            var msg = new Message(
                    "SqlFilterTopic",
                    s[0],
                    ("Hello RocketMQ " + i).getBytes()
            );
            // 同步发送，需要等待broker确认
            SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    int id = (Integer) o;
                    return list.get(id % list.size());
                }
            }, i);
            System.out.printf("Sent message: %s%n", sendResult);
        }


        System.out.println("over");
        System.in.read();

        // 3. 关闭生产者
        producer.shutdown();
    }
}